package com.medical.redis.config;

import com.medical.redis.locker.DistributedLocker;
import com.medical.redis.locker.RedissonDistributedLocker;
import com.medical.redis.utils.RedisUtil;
import com.medical.redis.utils.RedissLockUtil;
import jakarta.annotation.Resource;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RedisConfig {

    @Resource
    RedisProperties redisProperties;
    /**
     * 自定义 RedisTemplate
     * 修改 Redis 序列化方式，默认 JdkSerializationRedisSerializer
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    @ConditionalOnMissingBean
    public RedisConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory();
    }

    @Bean
    @ConditionalOnProperty(name = "spring.redis")
    public RedissonClient getRedissonClient() {
        Config config = new Config();
        RedisProperties.Cluster cluster = redisProperties.getCluster();
        String password = redisProperties.getPassword();
        if (cluster != null && cluster.getNodes() != null) {
            List<String> clusterNodes = new ArrayList<>();
            for (int i = 0; i < redisProperties.getCluster().getNodes().size(); i++) {
                clusterNodes.add("redis://" + redisProperties.getCluster().getNodes().get(i));
            }
            ClusterServersConfig clusterConfig = config.useClusterServers();
            clusterConfig.addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
            if (!StringUtils.isEmpty(password)) {
                clusterConfig.setPassword(password);
            }
        } else {
            SingleServerConfig singleConfig = config.useSingleServer();
            singleConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
                    .setDatabase(redisProperties.getDatabase());
            if (!StringUtils.isEmpty(password)) {
                singleConfig.setPassword(password);
            }
        }
        return Redisson.create(config);
    }


    @Bean
    @ConditionalOnBean(RedissonClient.class)
    DistributedLocker distributedLocker(RedissonClient redissonClient) {
        RedissonDistributedLocker locker = new RedissonDistributedLocker();
        locker.setRedissonClient(redissonClient);
        RedissLockUtil.setLocker(locker);
        return locker;
    }

    @Bean
    @ConditionalOnBean(RedisTemplate.class)
    public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate) {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        return redisUtil;
    }
}
